package egy.rev;

import java.io.FileInputStream;
import java.lang.reflect.Modifier;

import android.content.Context;
import android.graphics.AvoidXfermode.Mode;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.app.Activity;

public class ImageAdapter extends BaseAdapter {
	private Context myCont;
	int mGalleryItemBackground;
	private FileInputStream fis;
	private ImageView[] mImages;
	
	public ImageAdapter(Context c)
	{
		myCont = c;
		mImages = new ImageView[mThumbIds.length];
	}
	
	
	public boolean createReflectedImages() {
         //The gap we want between the reflection and the original image
         final int reflectionGap = 4;
         
         
         int index = 0;
         for (int imageId : mThumbIds) 
         {
         
          Bitmap originalImage = BitmapFactory.decodeResource(myCont.getResources(),imageId);
          int width = originalImage.getWidth();
          int height = originalImage.getHeight();
          
    
          //This will not scale but will flip on the Y axis
          Matrix matrix = new Matrix();
          matrix.preScale(1, -1);
          
          //Create a Bitmap with the flip matrix applied to it.
          //We only want the bottom half of the image
          Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
          
              
          //Create a new bitmap with same width but taller to fit reflection
          Bitmap bitmapWithReflection = Bitmap.createBitmap(width 
            , (height + height/2), Config.ARGB_8888);
        
         //Create a new Canvas with the bitmap that's big enough for
         //the image plus gap plus reflection
         Canvas canvas = new Canvas(bitmapWithReflection);
         //Draw in the original image
         canvas.drawBitmap(originalImage, 0, 0, null);
         //Draw in the gap
         Paint deafaultPaint = new Paint();
         canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
         //Draw in the reflection
         canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);
         
         //Create a shader that is a linear gradient that covers the reflection
         Paint paint = new Paint(); 
         LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, 
           bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, 
           TileMode.CLAMP); 
         //Set the paint to use this shader (linear gradient)
         paint.setShader(shader); 
         //Set the Transfer mode to be porter duff and destination in
         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
         //Draw a rectangle using the paint with our linear gradient
         canvas.drawRect(0, height, width, 
           bitmapWithReflection.getHeight() + reflectionGap, paint); 
         
         ImageView imageView = new ImageView(myCont);
         imageView.setImageBitmap(bitmapWithReflection);
         android.widget.Gallery.LayoutParams imgLayout = new CoverFlow.LayoutParams(320, 480);
         imageView.setLayoutParams(imgLayout);
         imageView.setPadding(30, 100, 20, 20);
         mImages[index++] = imageView;
         
         }
      return true;
 }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

     //Use this code if you want to load from resources
        ImageView i = new ImageView(myCont);
        i.setImageResource(mThumbIds[position]);
        i.setLayoutParams(new CoverFlow.LayoutParams(380, 450));
        i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
        
        //Make sure we set anti-aliasing otherwise we get jaggies
        BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
        drawable.setAntiAlias(true);
        return i;
     
     //return mImages[position];
    }
  /** Returns the size (0.0f to 1.0f) of the views 
     * depending on the 'offset' to the center. */ 
     public float getScale(boolean focused, int offset) { 
       /* Formula: 1 / (2 ^ offset) */ 
         return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
     } 

	
	// references to our images
    static public Integer[] mThumbIds = {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7,
            R.drawable.pic8,
            R.drawable.pic9,
            R.drawable.pic10,
            R.drawable.pic11,
            R.drawable.pic12,
            R.drawable.pic13,
            R.drawable.pic14,
            R.drawable.pic15,
            R.drawable.pic16,
            R.drawable.pic17,
            R.drawable.pic18,
            R.drawable.pic19,
            R.drawable.pic20,
            R.drawable.pic21,
            R.drawable.pic22,
            R.drawable.pic23,
            R.drawable.pic24,
            R.drawable.pic25,
            R.drawable.pic26,
            R.drawable.pic27,
            R.drawable.pic28,
            R.drawable.pic29,
            R.drawable.pic30,
            R.drawable.pic31,
            R.drawable.pic32,
            R.drawable.pic33,
            R.drawable.pic34,
            R.drawable.pic35,
            R.drawable.pic36,
            R.drawable.pic37,
            R.drawable.pic38,
            R.drawable.pic39,
            R.drawable.pic40,
            R.drawable.pic41,
            R.drawable.pic42,
            R.drawable.pic43,
            R.drawable.pic44,
            R.drawable.pic45,
            R.drawable.pic46,
            R.drawable.pic47,
            R.drawable.pic48,
            R.drawable.pic49,
            R.drawable.pic50,
            R.drawable.pic51,
            R.drawable.pic52,
            R.drawable.pic53,
            R.drawable.pic54,
            R.drawable.pic55,
            R.drawable.pic56,
            R.drawable.pic57,
            R.drawable.pic58,
            R.drawable.pic59,
            R.drawable.pic60,
            R.drawable.pic61,
            R.drawable.pic62,
            R.drawable.pic63,
            R.drawable.pic64,
            R.drawable.pic65,
            R.drawable.pic66,
            R.drawable.pic67,
            R.drawable.pic68,
            R.drawable.pic69,
            R.drawable.pic70,
            R.drawable.pic71,
            R.drawable.pic72,
            R.drawable.pic73,
            R.drawable.pic74,
            R.drawable.pic75,
            R.drawable.pic76,
            R.drawable.pic77,
            R.drawable.pic78,
            R.drawable.pic79,
            R.drawable.pic80,
            R.drawable.pic81,
            R.drawable.pic82,
            R.drawable.pic83,
            R.drawable.pic84,
            R.drawable.pic85,
            R.drawable.pic86,
            R.drawable.pic87,
            R.drawable.pic88,
            R.drawable.pic89,
            R.drawable.pic90,
            R.drawable.pic91,
            R.drawable.pic92,
            R.drawable.pic93,
            R.drawable.pic94,
            R.drawable.pic95,
            R.drawable.pic96,
            R.drawable.pic97,
            R.drawable.pic98,
            R.drawable.pic99,
            R.drawable.pic100,
            R.drawable.pic101,
            R.drawable.pic102,
            R.drawable.pic103,
            R.drawable.pic104
    };

}
